package examples.example1;

import dk.au.cs.peterbn.annotations.Member;
import dk.au.cs.peterbn.annotations.Members;
import dk.au.cs.peterbn.annotations.Pattern;
import dk.au.cs.peterbn.annotations.Patterns;

@Patterns({
    @Pattern(role="Strategy", name="gof:Strategy", id="RateStrategy"),
    @Pattern(role="State", name="gof:State", id="AlternatingStrategyState") })
public interface RateStrategy {

    @Members({
        @Member(role="AlgorithmInterface", id="RateStrategy"),
        @Member(role="Handle", id="AlternatingStrategyState")
    })
    int calculateTime(int amount);
}

@Patterns({
    @Pattern(role="ConcreteStrategy", name="gof:Strategy", id="RateStrategy"),
    @Pattern(role="Context", name="gof:State", id="AlternatingStrategyState"),
    @Pattern(role="ConcreteState", name="gof:State", id="AlternatingStrategyState") })
class AlternatingRateStrategy implements RateStrategy {
    private RateStrategy weekendStrategy, weekdayStrategy, currentState;

    public AlternatingRateStrategy( RateStrategy weekdayStrategy, RateStrategy weekendStrategy) {
        this.weekdayStrategy = weekdayStrategy;
        this.weekendStrategy = weekendStrategy;
        this.currentState = null;
    }

    @Override
    @Member(role="Request", id="AlternatingStrategyState")
    public int calculateTime(int amount) {
        if ( isWeekend() ) {
            currentState = weekendStrategy;
        } else {
            currentState = weekdayStrategy;
        }
        return currentState.calculateTime(amount);
    }

    private boolean isWeekend() {
        return true; //Should actually compute if it is weekend, but this is an optimistic approximation
    }
}
